package cn.tedu._04weibo.controller;

import cn.tedu._04weibo.base.response.JsonResult;
import cn.tedu._04weibo.base.response.StatusCode;
import cn.tedu._04weibo.mapper.UserMapper;
import cn.tedu._04weibo.pojo.datatransferobject.UserLoginParam;
import cn.tedu._04weibo.pojo.datatransferobject.UserRegParam;
import cn.tedu._04weibo.pojo.entity.User;
import cn.tedu._04weibo.pojo.valueobject.UserVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@RequestMapping("/v1/user/")
@Tag(name = "01-用户模块")
@Slf4j
public class UserController {
    @Autowired
    private UserMapper userMapper;

    @PostMapping("reg")
    @Operation(summary = "01-注册功能")
    @ApiOperationSupport(order = 10)
    public JsonResult regUser(@Validated @RequestBody UserRegParam userRegParam) {
        log.debug("userRegParam:" + userRegParam);
        String username = userRegParam.getUsername();
        if (username == null || username == "") {
            return new JsonResult(StatusCode.OPERATION_FAILED);
        } else {
            int num = userMapper.selectByUserName(username);
            if (num > 0) {
                return new JsonResult(StatusCode.USERNAME_EXIST);
            } else {
                User user = new User();
                BeanUtils.copyProperties(userRegParam, user);
                user.setCreateTime(new Date());
                log.debug("user:" + user);
                int nums = userMapper.insertUser(user);
                if (nums > 0) {
                    return JsonResult.Bingo();
                } else {
                    return new JsonResult(StatusCode.OPERATION_FAILED);
                }
            }
        }
    }

    @PostMapping("login")
    @Operation(summary = "02-登录功能")
    @ApiOperationSupport(order = 20)
    public JsonResult loginUser(@RequestBody UserLoginParam userLoginParam, HttpSession session) {
        log.debug("userLoginParam:" + userLoginParam);
        UserVO userVO = userMapper.selectByUAndP(userLoginParam);
        if (userVO == null) {
            return new JsonResult(StatusCode.NOT_LOGIN);
        } else {
            session.setAttribute("user", userVO);
            return JsonResult.Bingo(userVO);
        }
    }

    @GetMapping("currentUser")
    @Operation(summary = "03-获取用户功能")
    @ApiOperationSupport(order = 30)
    public JsonResult current(HttpSession session) {
        UserVO userVO = (UserVO) session.getAttribute("user");
        return JsonResult.Bingo(userVO);
    }

    @GetMapping("logout")
    @Operation(summary = "04-退出登录")
    @ApiOperationSupport(order = 40)
    public void logoutUser(HttpSession session) {
        session.removeAttribute("user");
    }
}
